package cn.chenlh.fx.component;

import javafx.scene.Scene;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Msg extends Dialog<ButtonType> {
    private double xOffSet = 0;
    private double yOffSet = 0;
    private Msg() {
        ButtonType okBtn = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        ButtonType cancelBtn = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);
        this.getDialogPane().getButtonTypes().addAll(cancelBtn, okBtn);
        this.setTitle("提示");
        this.setIcon("info.png");
        this.initStyle(StageStyle.UNDECORATED);

        Stage s = (Stage) this.getDialogPane().getScene().getWindow();


        StackPane root = new StackPane();

        root.setOnMousePressed(event -> {
            xOffSet = event.getSceneX();
            yOffSet = event.getSceneY();
        });

        root.setOnMouseDragged(event -> {
            s.setX(event.getScreenX() - xOffSet);
            s.setY(event.getScreenY() - yOffSet);
        });
        root.setStyle("-fx-background: red;");
        Text text = new Text("你确定???");
        root.getChildren().add(text);
        Scene scene = new Scene(root, 300, 100);
        s.setScene(scene);
    }

    public void setIcon(String imgPath) {
        setIcon(new Image(imgPath));
    }

    public void setIcon(Image image) {
        Stage s = (Stage) this.getDialogPane().getScene().getWindow();
        s.getIcons().add(image);
    }

    public static boolean of(String content) {
        Msg tips = new Msg();
        tips.setContentText(content);
        return tips.showAndWait().orElse(ButtonType.NO).getButtonData()
                .equals(ButtonType.OK.getButtonData());
    }
}
